#!/usr/bin/env node
const fs = require('fs');
const path = require('path');

(async function main(){
  try {
    const projectRoot = path.resolve(__dirname, '..');
    const src = path.join(projectRoot, 'node_modules', 'unenv', 'dist', 'runtime', 'mock', 'empty.mjs');
    const destDir = path.join(projectRoot, 'node_modules', 'unenv', 'dist', 'runtime', 'runtime', 'mock');
    const dest = path.join(destDir, 'empty.mjs');

    await fs.promises.mkdir(destDir, { recursive: true });

    if (fs.existsSync(src)) {
      // Preferred: copy existing empty.mjs if present in the installed package
      await fs.promises.copyFile(src, dest);
      console.info('[fix-unenv] copied empty.mjs to', dest);
    } else {
      // Fallback: create a minimal empty module so imports don't fail
      const fallbackContent = "// fallback empty module created by fix-unenv\nexport default {}\n";
      await fs.promises.writeFile(dest, fallbackContent, 'utf8');
      console.info('[fix-unenv] created fallback empty.mjs at', dest);
    }
  } catch (err) {
    console.error('[fix-unenv] error while attempting to fix unenv path:', err && err.stack ? err.stack : err);
  }
})();
